import Vue from 'vue'
import Vuex from 'vuex'
import app from './modules/app'
import user from './modules/user'
import tagsView from './modules/tagsView'
import permission from './modules/permission'
import settings from './modules/settings'
import getters from './getters'

import {loadModules,} from 'esri-loader';
const options = {
  url: 'http://218.94.9.8:18080/arcgis_js_api/4.11/dojo/dojo.js',
};
//const layerURL = 'https://sampleserver6.arcgisonline.com/arcgis/rest/services/USA/MapServer';
const layerURL = 'https://www.igiseis.com:6443/arcgis/rest/services/TuhaService/GISMap_Obstacle/MapServer';


Vue.use(Vuex)

const store = new Vuex.Store({
  modules: {
    app,
    user,
    tagsView,
    permission,
    settings
  },
  getters,

  state: {
    map: null,//用于存放应用中的map实例
    view: null,//用于存放应用中的view实例
    layerCount: 0,//用于记录图层数量
  },
  mutations: {
    //将在map组件里面实例化的map对state中的map进行初始化
    initMap(state, payload) {
      state.map = payload;
    },
    //与map类似，就是初始化view
    initView(state, payload) {
      state.view = payload;
    },
  },
  actions: {
    //两个方法增加图层和删除所有图层
    async addLayer({
      state,
    })
    {
      const layer = await loadModules(['esri/layers/MapImageLayer'], options)
        .then(([MapImageLayer]) => new MapImageLayer({
          url: layerURL,
        }))
        .catch((err) => {
          console.error(err);
        });
      state.map.add(layer);
      layer.when(() => {
        window.alert('地图加载成功！');
        state.layerCount = state.map.layers.length;
      });
    },
    reomveAllLayer({ state }) {
      if (this.state.map.layers) {
        this.state.map.removeAll();
        state.layerCount = 0;
        window.alert('地图删除成功！');
      }
    },
  },

})

export default store
